package com.jielihaofeng.site.category.controller;

import com.jielihaofeng.site.category.convert.CategoryConvert;
import com.jielihaofeng.site.category.entity.CategoryEntity;
import com.jielihaofeng.site.category.query.CategoryQuery;
import com.jielihaofeng.site.category.service.CategoryService;
import com.jielihaofeng.site.category.vo.CategoryVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import com.jielihaofeng.framework.common.constant.Constant;
import com.jielihaofeng.framework.common.page.PageResult;
import com.jielihaofeng.framework.common.utils.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 分类管理
 *
 * @author 借力好风 949394048@qq.com
 * @since 1.0.0 2022-07-21
 */
@RestController
@RequestMapping("site/category")
@Tag(name = "分类管理")
@AllArgsConstructor
public class CategoryController {
    private final CategoryService categoryService;

    @GetMapping("list")
    @Operation(summary = "列表")
    @PreAuthorize("hasAuthority('site:category:page')")
    public Result<List<CategoryVO>> list() {
        List<CategoryVO> list = categoryService.getList();

        return Result.ok(list);
    }

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('site:category:page')")
    public Result<PageResult<CategoryVO>> page(@Valid CategoryQuery query) {
        PageResult<CategoryVO> page = categoryService.page(query);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('site:category:info')")
    public Result<CategoryVO> get(@PathVariable("id") Long id) {
        CategoryEntity entity = categoryService.getById(id);
        CategoryVO vo = CategoryConvert.INSTANCE.convert(entity);
        // 获取上级分类名称
        if (entity != null && !Constant.ROOT.equals(entity.getPid())) {
            CategoryEntity parentEntity = categoryService.getById(entity.getPid());
            vo.setParentName(parentEntity.getName());
        }
        return Result.ok(vo);
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('site:category:save')")
    public Result<String> save(@RequestBody CategoryVO vo) {
        categoryService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('site:category:update')")
    public Result<String> update(@RequestBody @Valid CategoryVO vo) {
        categoryService.update(vo);

        return Result.ok();
    }

    @DeleteMapping("{id}")
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('site:category:delete')")
    public Result<String> delete(@PathVariable("id") Long id) {
        categoryService.delete(id);

        return Result.ok();
    }
}